<template>
    <div class="list-container">
        <div class='search-area'>
            <div class='search-left'>
                <div class='input-line'>
                    <span class='label'>搜索关键词：</span>
                    <el-input placeholder="请输入商户名" label='商户名' v-model="searchParams.keyword"/>
                </div>
            </div>
            <div class='search-right'>
                <el-button type="primary" @click='onSearchClick'>查询</el-button>
                <el-button type="primary" @click='onAddTypeClick'>新增类型</el-button>
            </div>
        </div>
        <el-table
            fit
            :data="tableData"
            style="width: 100%">
            <el-table-column
                prop="id"
                label="类型ID">
            </el-table-column>
            <el-table-column
                prop="typeName"
                label="类型名称">
            </el-table-column>
            <el-table-column
                align='center'
                label="操作"
                width='200'>
                <template slot-scope="scope">
                    <div style='display: flex;align-items: center;justify-content: center'>
                        <el-button style='flex-shrink: 0' type='danger' size='mini' @click="onDeleteClick(scope.row.id)">删除</el-button>
                        <el-button style='flex-shrink: 0' type='primary' size='mini' @click="onUpdateClick(scope.row.id)">修改</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <div class='pagination'>
            <el-pagination
                background
                layout="prev, pager, next"
                :current-page='currentPage'
                :page-size='10'
                :total="total"
                @current-change='onPageChange'
            >
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import { getArticleTypes, addArticleType, updateArticleType, deleteArticleType } from '@/api/article'
    export default {
        data() {
            return {
                searchParams: {
                        pageIndex: 1, // 分页号
                        pageSize: 10, // 单页数据条数
                        keyword: '', // 搜索关键词
                },
                currentPage: 1,
                total: 0,
                tableData: [],
                addFormData: {  // 添加类型参数
                    typeName: '', // 类型名称
                },
            }
        },
        mounted() {
            this.getTypeList()
        },
        methods: {
            onPageChange(page) {
                this.currentPage = page
            },
            // 获取资讯类型列表
            async getTypeList(currentPage) {
                let { searchParams } = this
                searchParams.pageIndex = currentPage || this.currentPage
                let res = await getArticleTypes(searchParams)
                this.tableData = res.list
                this.total = +res.total
                this.currentPage = +res.pageIndex
            },

            onSearchClick() {
                this.getTypeList(1)
            },

            // 添加类型
            onAddTypeClick() {
                this.$prompt('请输入类型名称', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(async ({ value }) => {
                    let requestParams = { typeName: value }
                    await addArticleType(requestParams)
                    this.getTypeList(1)
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消输入'
                    })
                })
            },

            // 修改类型
            onUpdateClick(id) {
                this.$prompt('请输入类型名称', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(async ({ value }) => {
                    let requestParams = { typeName: value, id }
                    await updateArticleType(requestParams)
                    this.getTypeList(1)
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消输入'
                    })
                })
            },

            // 删除类型
            onDeleteClick(id) {
                this.$confirm('此操作将永久删除该类型, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const data = await deleteArticleType({id})
                    this.$message.success('删除成功')
                    this.getTypeList(1)
                })
            }
        }
    }
</script>
<style lang='scss'>
    .list-container{
        .search-area{
            display: flex;
            align-items: center;
            width: 100%;
            padding: 20px 0;
            border-bottom: 1px solid #f2f2f2;
            .search-left{
                display: flex;
                align-items: center;
                justify-content: center;
                .input-line{
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin-right: 20px;
                    .label{
                        flex-shrink: 0;
                    }
                    .gap{
                        padding: 0 10px;
                    }
                }
            }
            .search-right{
                display: flex;
                justify-content: flex-end;
                margin-left: 20px;
            }
        }
        .pagination{
            margin-top: 20px;
        }
    }
</style>
